package com.gxc.array;

/**
 * Created by ASD on 2024/7/20.
 */
public class MinimumMoves {

    public static void main(String[] args) {
/*        System.out.println(handle(new int[][]{
                {1,1,0},
                {1,1,1},
                {1,2,1}
        }));

        System.out.println(handle(new int[][]{
                {1,3,0},
                {1,0,0},
                {1,0,3}
        }));*/

        System.out.println(handle(new int[][]{
                {3,2,0},
                {0,1,0},
                {0,3,0}
        }));
    }

    public static int handle(int[][] grid) {
        int res = 0;

        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == 0) {
                    int min = 4;
                    for (int m = 0; m < grid.length; m++) {
                        for (int n = 0; n < grid[m].length; n++) {
                            if (grid[m][n] > 1) {
                                int move = Math.abs(m-i) + Math.abs(n-j);
                                min = Math.min(move, min);
                            }
                        }
                    }
                    res += min;
                }
            }
        }
        return res;
    }

}
